package com.atguigu.day16.array02;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/19 15:54
 * @注释
 */
public class StudentTest {
    Student[] arr;

    @Before
    public void before() {
        Student s = new Student("张三", 15, 99.5);
        Student s1 = new Student("李四", 20, 99.3);
        Student s2 = new Student("王五", 19, 99.9);
        Student s3 = new Student("赵六", 14, 99.7);

        arr = new Student[]{s, s1, s2, s3};

        System.out.println("排序前");
        for (Student student : arr) {
            System.out.println(student);
        }
    }

    @After
    public void after() {
        System.out.println("排序后");
        for (Student student : arr) {
            System.out.println(student);
        }
    }

    @Test
    public void test01() {
        Arrays.sort(arr);
    }

    @Test
    public void test02(){
        SortStudentByScore byScore = new SortStudentByScore();
        Arrays.sort(arr, byScore);
    }
    @Test
    public void test03(){
        SortStudentByScore byScore = new SortStudentByScore();
        Arrays.sort(arr,new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return -Double.compare(o1.score,o2.score);
            }
        });
    }
}
class SortStudentByScore implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return Double.compare(o1.score, o2.score);
    }
}
